﻿using Microsoft.EntityFrameworkCore;
using Org.BouncyCastle.Asn1.X509;
using WallpaperDemo.Models;

namespace WallpaperDemo
{
    public class WallpaperService
    {
        private int numAp = 10;
        private readonly WallpapersContext db;
        public WallpaperService(WallpapersContext context)
        {
            db = context;
        }

        public List<Wallpaper> Wallpapers
        {
            get
            {
                return db.Wallpapers
                       .Include(o => o.WUpdater).Include(o => o.WTags)
                       .ToList<Wallpaper>();
            }
        }

        public void AddWallpaper(Wallpaper wallpaper)
        {
            wallpaper.WCreateTime = DateTime.Now;
            wallpaper.WallpaperId = Guid.NewGuid().ToString();
            wallpaper.WTags.ForEach(o => { o.TagId = Guid.NewGuid().ToString(); });
            db.Wallpapers.Add(wallpaper);
            db.SaveChanges();
        }

        public void RemoveWallpaper(string id)
        {
            var q = db.Wallpapers
                .Include(o => o.WTags)
                .SingleOrDefault(o => o.WallpaperId == id);
            if (q == null) return;
            db.Tags.RemoveRange(q.WTags);
            db.Wallpapers.Remove(q);
            db.SaveChanges();
        }

        public Wallpaper QueryWallpaper_Id(string id)
        {
            var query = db.Wallpapers
                        .Include(o => o.WUpdater).Include(o => o.WTags)
                        .SingleOrDefault(o => o.WallpaperId == id);
            return query;
        }

        public List<Wallpaper> QueryWallpaper_User(string username)
        {
            var query = db.Wallpapers.Include(o => o.WUpdater).Include(o => o.WTags)
                        .Where(o => o.WUpdater.UserName == username);
            return query.ToList();
        }

        public List<Wallpaper> QueryWallpaper_Title(string title)
        {
            var query = db.Wallpapers.Include(o => o.WUpdater).Include(o => o.WTags)
                        .Where(o => o.WTitle == title);
            return query.ToList();
        }

        public List<Wallpaper> QueryWallpaper_Cat(string cat, int seq)
        {
            var query = db.Wallpapers.Include(o => o.WUpdater).Include(o => o.WTags)
                        .Where(o => o.WCate == cat)
                        .Skip(seq *numAp).Take(numAp);
            return query.ToList();
        }
        
        public List<Wallpaper> QueryWallpaper_Search(string keyword, int seq)
        {
            string[] arr = keyword.Split('+');
            int len = arr.Length;
            List<Wallpaper> query = new List<Wallpaper>();
            for(int i=0; i<len; i++)
            {
                var q = QueryWallpaper_SingleKey(arr[i], seq/len);
                query.Concat(q);
            }
            return query;
        }

        public List<Wallpaper> QueryWallpaper_SingleKey(string keyword, int seq)
        {
            var query = db.Wallpapers.Include(w => w.WTags)
                        .Where(w => w.WTags.Any(t => EF.Functions.Like(t.TagName, "%" + keyword + "%")))
                        .Skip(seq * numAp).Take(numAp);
            //if(res.Count < numAp) { res.Concat(GetFuzzy1(keyword, numAp - res.Count)); }
            
            return query.ToList();
        }
        /*
        public List<Wallpaper> GetFuzzy1(string keyword, int num)
        {
            var query = db.Wallpapers.Include(o => o.WTags).Except(
                        db.Wallpapers
                        .Include(o => o.WTags)
                        .Where(o => o.WTags.Any(t => t.TagName.Contains(keyword))))
        }*/
    }
}
